/*
 * inity.c
 *
 *  Created on: Apr 12, 2012
 *      Author: Rafal Sokolowski
 *      Comment: Obsluga przyciskow lacznie z funkcjami obslugujacymi przerwania.
 */

#include "inity.h"
#include "usart.h"
#include "board.h"
#include "gpio.h"
#include "tmry.h"
#include "tc.h"
#include "intc.h"

#include "uarty.h"
#include "frame.h"
#include "btny.h"
#include "ledy.h"

#define TC_CHANNEL    0


void init(volatile avr32_tc_t *tc)
{
	g_u16TimerServicesPointer = 0; // init globalnej zmiennej do timerSchedulera
	g_oIncomingFrame.u16_MsgIsFresh = 0; // init globalnej zmiennej do USART - RX
	g_oOutcomingFrame.u16_MsgIsReadyToGo = 0; // init globalnej zmiennej do USART - TX
	g_i32changeSM_timerID = timerRegister(changeSM);
	g_i32Led_timerID = timerRegister(timerLed);
	g_oFlags.u8_MasterSlave = 0; // startuje def. w Slave Mode

	static const gpio_map_t USART_GPIO_MAP =
	{
	   {AVR32_USART1_RXD_0_PIN, AVR32_USART1_RXD_0_FUNCTION},
	   {AVR32_USART1_TXD_0_PIN, AVR32_USART1_RXD_0_FUNCTION},
	   {AVR32_USART0_RXD_0_PIN, AVR32_USART0_RXD_0_FUNCTION},
	   {AVR32_USART0_TXD_0_PIN, AVR32_USART0_TXD_0_FUNCTION}
	};

	static const usart_options_t USART_OPTIONS =
	{
	   .baudrate     = 9600,
	   .charlength   = 8,
	   .paritytype   = USART_NO_PARITY,
	   .stopbits     = USART_1_STOPBIT,
	   .channelmode  = USART_NORMAL_CHMODE
	};

	static const tc_waveform_opt_t WAVEFORM_OPT =
	{
	   .channel  = TC_CHANNEL,                        // Channel selection.

	   .bswtrg   = TC_EVT_EFFECT_NOOP,                // Software trigger effect on TIOB.
	   .beevt    = TC_EVT_EFFECT_NOOP,                // External event effect on TIOB.
	   .bcpc     = TC_EVT_EFFECT_NOOP,                // RC compare effect on TIOB.
	   .bcpb     = TC_EVT_EFFECT_NOOP,                // RB compare effect on TIOB.

	   .aswtrg   = TC_EVT_EFFECT_NOOP,                // Software trigger effect on TIOA.
	   .aeevt    = TC_EVT_EFFECT_NOOP,                // External event effect on TIOA.
	   .acpc     = TC_EVT_EFFECT_NOOP,                // RC compare effect on TIOA: toggle.
	   .acpa     = TC_EVT_EFFECT_NOOP,                // RA compare effect on TIOA: toggle (other possibilities are none, set and clear).

	   .wavsel   = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER,// Waveform selection: Up mode with automatic trigger(reset) on RC compare.
	   .enetrg   = FALSE,                             // External event trigger enable.
	   .eevt     = 0,                                 // External event selection.
	   .eevtedg  = TC_SEL_NO_EDGE,                    // External event edge selection.
	   .cpcdis   = FALSE,                             // Counter disable when RC compare.
	   .cpcstop  = FALSE,                             // Counter clock stopped with RC compare.

	   .burst    = FALSE,                             // Burst signal selection.
	   .clki     = FALSE,                             // Clock inversion.
	   .tcclks   = TC_CLOCK_SOURCE_TC5                // Internal source clock 3, connected to fPBA / 8.
	};

	static const tc_interrupt_t TC_INTERRUPT =
	{
	   .etrgs = 0,
	   .ldrbs = 0,
	   .ldras = 0,
	   .cpcs  = 1,
	   .cpbs  = 0,
	   .cpas  = 0,
	   .lovrs = 0,
	   .covfs = 0
	};

	pm_switch_to_osc0(&AVR32_PM, 12000000, OSC0_STARTUP); //zmiana oscylatora

    // Assign GPIO to USART.
	gpio_enable_module(USART_GPIO_MAP,sizeof(USART_GPIO_MAP) / sizeof(USART_GPIO_MAP[0]));


	gpio_enable_pin_interrupt(GPIO_JOYSTICK_PUSH , GPIO_RISING_EDGE);
	gpio_enable_pin_interrupt(GPIO_JOYSTICK_LEFT , GPIO_FALLING_EDGE);
	gpio_enable_pin_interrupt(GPIO_JOYSTICK_RIGHT , GPIO_FALLING_EDGE);
	gpio_enable_pin_interrupt(GPIO_JOYSTICK_UP , GPIO_FALLING_EDGE);
	gpio_enable_pin_interrupt(GPIO_JOYSTICK_DOWN , GPIO_FALLING_EDGE);
	gpio_enable_pin_interrupt(GPIO_PUSH_BUTTON_0, GPIO_FALLING_EDGE);
	gpio_enable_pin_interrupt(GPIO_PUSH_BUTTON_1, GPIO_FALLING_EDGE);
	gpio_enable_pin_interrupt(GPIO_PUSH_BUTTON_2, GPIO_FALLING_EDGE);

	usart_init_rs232((&AVR32_USART1), &USART_OPTIONS, 12000000);
	usart_init_rs232((&AVR32_USART0), &USART_OPTIONS, 12000000);

	Disable_global_interrupt();
	//nie usuwac, do wlaczenia po napisaniu obslugi przerwan
//TODO: ustawic rejestracje przerwan dla wybranych modulow
	INTC_register_interrupt( (__int_handler)&uartIntHandler,
			AVR32_USART0_IRQ, AVR32_INTC_INT3);
//	INTC_register_interrupt( (__int_handler)&interruptJoystick,
//			AVR32_GPIO_IRQ_0 + (GPIO_JOYSTICK_PUSH/8), AVR32_INTC_INT3);
	INTC_register_interrupt( (__int_handler)&interruptJoystick,
			AVR32_GPIO_IRQ_0 + (GPIO_JOYSTICK_PUSH/8), AVR32_INTC_INT3);
	INTC_register_interrupt( (__int_handler)&interruptJoystick,
				AVR32_GPIO_IRQ_0 + (GPIO_JOYSTICK_LEFT/8), AVR32_INTC_INT3);
	INTC_register_interrupt( (__int_handler)&interruptJoystick,
				AVR32_GPIO_IRQ_0 + (GPIO_JOYSTICK_RIGHT/8), AVR32_INTC_INT3);
	INTC_register_interrupt( (__int_handler)&interruptJoystick,
				AVR32_GPIO_IRQ_0 + (GPIO_JOYSTICK_UP/8), AVR32_INTC_INT3);
	INTC_register_interrupt( (__int_handler)&interruptJoystick,
				AVR32_GPIO_IRQ_0 + (GPIO_JOYSTICK_DOWN/8), AVR32_INTC_INT3);
	INTC_register_interrupt( (__int_handler)&interruptJoystick,
			AVR32_GPIO_IRQ_0 + (GPIO_PUSH_BUTTON_0/8), AVR32_INTC_INT3);
	INTC_register_interrupt( (__int_handler)&interruptJoystick,
			AVR32_GPIO_IRQ_0 + (GPIO_PUSH_BUTTON_1/8), AVR32_INTC_INT3);
	INTC_register_interrupt( (__int_handler)&interruptJoystick,
			AVR32_GPIO_IRQ_0 + (GPIO_PUSH_BUTTON_2/8), AVR32_INTC_INT3);
	INTC_register_interrupt(&interruptTimer, AVR32_TC_IRQ0, AVR32_INTC_INT0);
	//(&AVR32_USART1)->ier = AVR32_USART_IER_RXRDY_MASK; //tylko dla USART'a odbierajacego
	(&AVR32_USART0)->ier = AVR32_USART_IER_RXRDY_MASK; //tylko dla USART'a odbierajacego
	Enable_global_interrupt();


	tc_init_waveform(tc, &WAVEFORM_OPT);
	tc_write_rc(tc, TC_CHANNEL, (FOSC0/128)/4); //czestotliwosc zegara
	tc_configure_interrupts(tc, TC_CHANNEL, &TC_INTERRUPT);
	tc_start(tc, TC_CHANNEL);
};
